package cn.wildfire.chat.app.extendsion.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.app.extendsion.utils.TakeoverInterfaceHub;
import cn.wildfire.chat.app.main.TakeoverGroupListener;
import cn.wildfire.chat.kit.contact.BaseUserListFragment;
import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfire.chat.kit.utils.PinyinUtils;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * Create by HEKE, 2019/8/23 20:38
 * TODO 转让群主操作对象
 */
public class TakeoverGroupOwerFragment extends BaseUserListFragment {

    /**
     * 用来加载的用户数据列表
     */
    protected List<UIUserInfo> uiUserInfoArrayList;
    protected GroupInfo groupInfo;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    /**
     * 绑定数据
     *
     * @param groupInfo
     */
    public void setUiUserInfoArrayList(GroupInfo groupInfo) {
        this.groupInfo = groupInfo;
        try {
            List<GroupMember> groupMembersList = ChatManager.Instance().getGroupMembers(groupInfo.target, true);
            this.uiUserInfoArrayList = memberToUIUserInfo(groupMembersList);
            // 去除自身
            ArrayList<UIUserInfo> validList = new ArrayList<>();
            String selfId = ChatManager.Instance().getUserId();
            for (UIUserInfo uiUserInfo : uiUserInfoArrayList) {
                if (!uiUserInfo.getUserInfo().uid.equals(selfId)) {
                    validList.add(uiUserInfo);
                }
            }
            this.uiUserInfoArrayList = validList;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void takeoverset() {
        super.takeoverset();
        try {
            userListAdapter.setUsers(uiUserInfoArrayList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUserClick(UIUserInfo userInfo) {
        super.onUserClick(userInfo);
        try {
            TakeoverGroupListener takeoverGroupListener = TakeoverInterfaceHub.getTakeoverGroupListener();
            if (takeoverGroupListener != null) {
                takeoverGroupListener.callback(userInfo.getUserInfo());
            }
            getActivity().finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<UIUserInfo> memberToUIUserInfo(List<GroupMember> members) {

        List<String> memberIds = new ArrayList<>(members.size());
        for (GroupMember member : members) {
            memberIds.add(member.memberId);
        }

        List<UIUserInfo> uiUserInfos = new ArrayList<>();
        List<UIUserInfo> managerUiUserInfos = new ArrayList<>();
        List<UIUserInfo> normalUiUserInfos = new ArrayList<>();
        List<UserInfo> userInfos = UserViewModel.getUsers(memberIds, groupInfo.target);
        boolean showManagerCategory = false;
        for (UserInfo userInfo : userInfos) {
            UIUserInfo info = new UIUserInfo(userInfo);
            if (!TextUtils.isEmpty(userInfo.displayName)) {
                String pinyin = PinyinUtils.getPinyin(userInfo.displayName);
                char c = pinyin.toUpperCase().charAt(0);
                if (c >= 'A' && c <= 'Z') {
                    info.setSortName(("" + c).toUpperCase());
                } else {
                    // 为了让排序排到最后
                    info.setSortName("#");
                }
            } else {
                info.setSortName("#");
            }

            for (GroupMember member : members) {
                if (userInfo.uid.equals(member.memberId)) {
                    if (member.type == GroupMember.GroupMemberType.Manager) {
                        info.setCategory("管理员");
                        if (!showManagerCategory) {
                            showManagerCategory = true;
                            info.setShowCategory(true);
                        }
                        managerUiUserInfos.add(info);
                    } else {
                        info.setCategory(info.getSortName());
                        info.setShowCategory(true);
                        normalUiUserInfos.add(0, info);
                    }
                    break;
                }
            }
        }
        uiUserInfos.addAll(managerUiUserInfos);
        uiUserInfos.addAll(normalUiUserInfos);
        return uiUserInfos;
    }

}
